package day11;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author joker
 * @date 2022/4/21 0021 - 19:56
 */
public class CountCodeLine {
    public static void main(String[] args) {
        File file = new File("D:\\专业资料\\code\\科蓝\\code");
        File[] files = file.listFiles();
        System.out.println(findFiles(files,0));
    }
    public static int findFiles(File[] files,int lnum){
        int num=lnum;
        for (File file : files) {
            if(file.isDirectory()){
                File[] files1=file.listFiles();
                num=findFiles(files1,num);
            }else{
                if(file.getName().endsWith(".java")){
                    num+=countLines(file);
                }
            }
        }
        return num;
    }
    @Test
    public static int countLines(File file){
//        File file = new File("src/main/java/day11/Test03.java");
        int num=0;
        BufferedReader br = null;
        try {
            FileReader fr = new FileReader(file);
            br = new BufferedReader(fr);
            String str=null;
            while((str=br.readLine())!=null){
                if(!"".equals(str))
                num++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//        System.out.println(num);
        return num;
    }
}
